import React, { Component, Fragment } from 'react';
import { Card, Descriptions, Image, Button, Space, Modal, Radio, Form, Input, Row, Col, message, Select, Result } from 'antd'
import { connect } from 'dva';
const { Option } = Select;

const discountRateArr = ['3%', '4%', '5%', '6%', '7%', '8%', '9%', '10%']

const tailLayout = {
    wrapperCol: { offset: 8, span: 16 },
}

class TechnicalValuationForm extends React.Component {
    constructor(props) {
        super(props),
            this.state = {
                technologyValuationVisible: false,
                labelCol: {
                    span: 16
                },
                wrapperCol: {
                    span: 9
                },
                isQuantitativeValuation: true,       //是否是定性估值
                isInternalRD: true,                   //是否是内部引进
                residualLife: -1,
                result: "",
                isResult: false
            }
    }

    componentDidMount(){
        this.setState({
            isQuantitativeValuation:this.props.selectedKey.indexOf('qualitative') > -1 ? true : false
        })
    }

    renderResidualLifeForm = y => {
        if (y == -1) {
            return
        }
        let formArr = []
        for (let i = 0; i < y; i++) {
            formArr.push(
                <Form.Item
                    label={"预期第" + (i + 1) + "年现金流（万元）"}
                    name={(i + 1) + "yearProfit"}
                    rules={[{ required: true, message: '请输入预期第' + (i + 1) + '年现金流' }]}
                >
                    <Input style={{ width: 100 }} type='number'></Input>
                </Form.Item>)
        }

        return formArr
    }

    renderResult = isResult => {
        if (isResult) {
            return (
                <Result
                    status="success"
                    title={this.state.isQuantitativeValuation ? "定性估值" : "定量估值"}
                    subTitle={this.state.result}
                />
            )
        }
    }

    setResidualLife = e => {
        if (e.target.value > 5) {
            message.error("请输入5以内的数值！")
            return
        }
        this.setState({
            residualLife: e.target.value
        })
    }

    onFinish = values => {
        let result = 0;
        for (let key in values) {
            if (key == "remainingLegalLife") {
                if (values[key] == 1) {
                    result += 0.2 * 0.3 * 1 * 10
                }
                if (values[key] == 2) {
                    result += 0.2 * 0.3 * 1 * 7
                }
                if (values[key] == 3) {
                    result += 0.2 * 0.3 * 1 * 4
                }
                if (values[key] == 4) {
                    result += 0.2 * 0.3 * 1 * 2
                }
            }
            if (key == "independentClaimsNumber") {
                if (values[key] == 1) {
                    result += 0.2 * 0.7 * 1 * 10
                }
                if (values[key] == 2) {
                    result += 0.2 * 0.7 * 1 * 7
                }
                if (values[key] == 3) {
                    result += 0.2 * 0.7 * 1 * 4
                }
                if (values[key] == 4) {
                    result += 0.2 * 0.7 * 1 * 2
                }
            }
            if (key == "isPrototypeTechnology") {
                if (values[key] == 1) {
                    result += 0.4 * 0.3 * 0.4 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.3 * 0.4 * 4
                }
            }
            if (key == "RDBackground") {
                if (values[key] == 1) {
                    result += 0.4 * 0.3 * 0.2 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.3 * 0.2 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.3 * 0.2 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.3 * 0.2 * 2
                }
            }
            if (key == "technologyType") {
                if (values[key] == 1) {
                    result += 0.4 * 0.3 * 0.3 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.3 * 0.3 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.3 * 0.3 * 2
                }
            }
            if (key == "patentsCitedNumber") {
                if (values[key] == 1) {
                    result += 0.4 * 0.3 * 0.1 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.3 * 0.1 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.3 * 0.1 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.3 * 0.1 * 2
                }
            }
            if (key == "IPCClassificationNumber") {
                if (values[key] == 1) {
                    result += 0.4 * 0.2 * 1 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.2 * 1 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.2 * 1 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.2 * 1 * 2
                }
            }
            if (key == "technologyReadiness") {
                if (values[key] == 1) {
                    result += 0.4 * 0.3 * 0.5 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.3 * 0.5 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.3 * 0.5 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.3 * 0.5 * 2
                }
            }

            if (key == "completenessOfProcessParametersAndCertificates") {
                if (values[key] == 1) {
                    result += 0.4 * 0.3 * 0.5 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.3 * 0.5 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.3 * 0.5 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.3 * 0.5 * 2
                }
            }

            if (key == "similarPatentsNumber") {
                if (values[key] == 1) {
                    result += 0.4 * 0.2 * 0.6 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.2 * 0.6 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.2 * 0.6 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.2 * 0.6 * 2
                }
            }

            if (key == "technologyMaterialsUtilizationLevel") {
                if (values[key] == 1) {
                    result += 0.4 * 0.2 * 0.4 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.2 * 0.4 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.2 * 0.4 * 4
                }
            }

            if (key == "productsMarketDemand") {
                if (values[key] == 1) {
                    result += 0.4 * 0.5 * 0.4 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.5 * 0.4 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.5 * 0.4 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.5 * 0.4 * 2
                }
            }

            if (key == "productsMarketSize") {
                if (values[key] == 1) {
                    result += 0.4 * 0.5 * 0.3 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.5 * 0.3 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.5 * 0.3 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.5 * 0.3 * 2
                }
            }

            if (key == "productsMarketCompetition") {
                if (values[key] == 1) {
                    result += 0.4 * 0.5 * 0.3 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.5 * 0.3 * 4
                }
            }

            if (key == "economicPerformance") {
                if (values[key] == 1) {
                    result += 0.4 * 0.5 * 1 * 10
                }
                if (values[key] == 2) {
                    result += 0.4 * 0.5 * 1 * 7
                }
                if (values[key] == 3) {
                    result += 0.4 * 0.5 * 1 * 4
                }
                if (values[key] == 4) {
                    result += 0.4 * 0.5 * 1 * 2
                }
            }
        }
        if (this.state.isQuantitativeValuation) {
            this.setState({
                result: "定性估值：" + (result * 10).toFixed(2) + "%",
                isResult: true
            })
        }

        if (!this.state.isQuantitativeValuation) {
            let pmin = 0;
            let pmax = Number(values["1yearProfit"]);
            if (values.technologySources == 1) {
                pmin = ((Number(values.technologyCost) + Number(values.obtainingAndMaintainingPatentsCost) + Number(values.transactionCost)) * (Number(values.technologyRemainingYears) / (Number(values.technologyRemainingYears) + Number(values.technologyActualYears)))).toFixed(3)
            }

            if (values.technologySources == 2) {
                pmin = (Number(values.technologyPatentBookCost) * Number(values.priceIndexPurchase) * (Number(values.technologyRemainingYears) / (Number(values.technologyRemainingYears) * Number(values.technologyActualYears))) / Number(values.priceIndexEvaluation)).toFixed(3)
            }

            for (let i = 0; i < this.state.residualLife.length; i++) {
                pmax += Number(values[(i + 1) + "yearProfit"]) / (1 + (Number(values.discountRate) + 3) / 100)
            }
            pmax = Number(pmax).toFixed(3)
            this.setState({
                result: "定量估值：" + (Number(pmin) + (Number(result) / 10) * (Number(pmax) - Number(pmin))).toFixed(3),
                isResult: true
            })
        }
    }

    changeValuationType = (url) => {
        console.log(url)
        // if (url.indexOf('qualitative') > -1) {
        //     this.setState({
        //         isQuantitativeValuation: true,
        //         isResult:false
        //     })
        // }
        this.setState({
            isQuantitativeValuation: url.indexOf('qualitative') > -1 ? true : false,
            isResult:false
        })
    }

    changeTechnologySources = e => {
        if (e.target.value == 1) {
            this.setState({
                isInternalRD: true
            })
        }

        if (e.target.value == 2) {
            this.setState({
                isInternalRD: false
            })
        }
    }

    render() {
        return (
            <Form
                name="technologyPageValuation"
                onFinish={this.onFinish}
                onFinishFailed={this.onFinishFailed}
            >
            {/* {this.changeValuationType(this.props.selectedKey)} */}
                <Form.Item
                    label="剩余法律寿命"
                    name="remainingLegalLife"
                    rules={[{ required: true, message: '请选择剩余法律寿命' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>15年以上</Radio>
                        <Radio value={2}>10-15年</Radio>
                        <Radio value={3}>5-10年</Radio>
                        <Radio value={4}>5年以内</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="独立权利要求数"
                    name="independentClaimsNumber"
                    rules={[{ required: true, message: '请选择独立权利要求数' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>8项以上</Radio>
                        <Radio value={2}>5-7项</Radio>
                        <Radio value={3}>3-6项</Radio>
                        <Radio value={4}>5项以内</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="是否有原型技术"
                    name="isPrototypeTechnology"
                    rules={[{ required: true, message: '请选择是否有原型技术' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>无</Radio>
                        <Radio value={2}>有</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="研发背景"
                    name="RDBackground"
                    rules={[{ required: true, message: '请选择研发背景' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>产学合作</Radio>
                        <Radio value={2}>企业独立研发</Radio>
                        <Radio value={3}>高校等科研机构独立研发</Radio>
                        <Radio value={4}>个人</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="技术类型"
                    name="technologyType"
                    rules={[{ required: true, message: '请选择技术类型' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>发明授权专利</Radio>
                        <Radio value={2}>实用新型专利</Radio>
                        <Radio value={3}>外观设计专利</Radio>

                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="引证专利数"
                    name="patentsCitedNumber"
                    rules={[{ required: true, message: '请选择引证专利数' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>5项及以上</Radio>
                        <Radio value={2}>3-4项</Radio>
                        <Radio value={3}>1-2项</Radio>
                        <Radio value={4}>无</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="IPC分类号数"
                    name="IPCClassificationNumber"
                    rules={[{ required: true, message: '请选择IPC分类号数' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>5项以上</Radio>
                        <Radio value={2}>4-5项</Radio>
                        <Radio value={3}>3项</Radio>
                        <Radio value={4}>1-2项</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="技术就绪度"
                    name="technologyReadiness"
                    rules={[{ required: true, message: '请选择技术就绪度' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>8-9</Radio>
                        <Radio value={2}>6-7</Radio>
                        <Radio value={3}>4-5</Radio>
                        <Radio value={4}>1-3</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="工艺参数与证书的齐全度"
                    name="completenessOfProcessParametersAndCertificates"
                    rules={[{ required: true, message: '请选择工艺参数与证书的齐全度' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>完整</Radio>
                        <Radio value={2}>参数完整，证书不完整</Radio>
                        <Radio value={3}>参数不完整，证书不完整</Radio>
                        <Radio value={4}>都不完整</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="相似专利数"
                    name="similarPatentsNumber"
                    rules={[{ required: true, message: '请选择相似专利数' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>无</Radio>
                        <Radio value={2}>5项以内</Radio>
                        <Radio value={3}>6-10项</Radio>
                        <Radio value={4}>10项以上</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="技术对原料的利用水平"
                    name="technologyMaterialsUtilizationLevel"
                    rules={[{ required: true, message: '请选择技术对原料的利用水平' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>高值化利用</Radio>
                        <Radio value={2}>中值化利用</Radio>
                        <Radio value={3}>低值化利用</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="产品的市场需求量"
                    name="productsMarketDemand"
                    rules={[{ required: true, message: '请选择产品的市场需求量' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>很大</Radio>
                        <Radio value={2}>大</Radio>
                        <Radio value={3}>一般</Radio>
                        <Radio value={4}>小</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="产品的市场规模"
                    name="productsMarketSize"
                    rules={[{ required: true, message: '请选择产品的市场规模' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>很大</Radio>
                        <Radio value={2}>大</Radio>
                        <Radio value={3}>一般</Radio>
                        <Radio value={4}>小</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="产品的市场竞争程度"
                    name="productsMarketCompetition"
                    rules={[{ required: true, message: '请选择产品的市场竞争程度' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>弱</Radio>
                        <Radio value={2}>强</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="经济效益"
                    name="economicPerformance"
                    rules={[{ required: true, message: '请选择经济效益' }]}
                >
                    <Radio.Group>
                        <Radio value={1}>很高</Radio>
                        <Radio value={2}>高</Radio>
                        <Radio value={3}>较低</Radio>
                        <Radio value={4}>低</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="技术来源"
                    name="technologySources"
                    hidden={this.state.isQuantitativeValuation}
                    rules={[{ required: !this.state.isQuantitativeValuation, message: '请选择技术来源' }]}
                >
                    <Radio.Group onChange={this.changeTechnologySources}>
                        <Radio value={1}>内部研发</Radio>
                        <Radio value={2}>外部引进</Radio>
                    </Radio.Group>
                </Form.Item>
                <Row>
                    <Col span={8}>
                        <Form.Item
                            label="获得维持专利权成本（万元）"
                            name="obtainingAndMaintainingPatentsCost"
                            hidden={this.state.isQuantitativeValuation || !this.state.isInternalRD}
                            rules={[{ required: !(this.state.isQuantitativeValuation || !this.state.isInternalRD), message: '请输入获得和维持专利权的成本' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                    <Col span={8}>
                        <Form.Item
                            label="开发的技术成本（万元）"
                            name="technologyCost"
                            hidden={this.state.isQuantitativeValuation || !this.state.isInternalRD}
                            rules={[{ required: !(this.state.isQuantitativeValuation || !this.state.isInternalRD), message: '请输入开发的技术成本' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                    <Col span={8}>
                        <Form.Item
                            label="交易成本（万元）"
                            name="transactionCost"
                            hidden={this.state.isQuantitativeValuation || !this.state.isInternalRD}
                            rules={[{ required: !(this.state.isQuantitativeValuation || !this.state.isInternalRD), message: '请输入交易成本' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                </Row>
                <Row>
                    <Col span={8}>
                        <Form.Item
                            label="技术的专利账面成本（万元）"
                            name="technologyPatentBookCost"
                            hidden={this.state.isInternalRD}
                            rules={[{ required: !this.state.isInternalRD, message: '请输入技术专利账面成本' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                    <Col span={8}>
                        <Form.Item
                            label="评估时的物价指数"
                            name="priceIndexEvaluation"
                            hidden={this.state.isInternalRD}
                            rules={[{ required: !this.state.isInternalRD, message: '请输入评估时的物价指数' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                    <Col span={8}>
                        <Form.Item
                            label="购买时的物价指数"
                            name="priceIndexPurchase"
                            hidden={this.state.isInternalRD}
                            rules={[{ required: !this.state.isInternalRD, message: '请输入购买时的物价指数' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                </Row>
                <Row>
                    <Col span={8}>
                        <Form.Item
                            label="技术实际使用的年限（年）"
                            name="technologyActualYears"
                            hidden={this.state.isQuantitativeValuation}
                            rules={[{ required: !this.state.isQuantitativeValuation, message: '请输入技术实际使用的年限' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                    <Col span={8}>
                        <Form.Item
                            label="技术尚可使用的年限（年）"
                            name="technologyRemainingYears"
                            hidden={this.state.isQuantitativeValuation}
                            rules={[{ required: !this.state.isQuantitativeValuation, message: '请输入技术尚可使用的年限' }]}
                            labelCol={this.state.labelCol}
                            wrapperCol={this.state.wrapperCol}
                        >
                            <Input style={{ width: 100 }} type='number'></Input>
                        </Form.Item>
                    </Col>
                </Row>
                <Form.Item
                    label="剩余寿命（年）"
                    name="residualLife"
                    hidden={this.state.isQuantitativeValuation}
                    rules={[{ required: !this.state.isQuantitativeValuation, message: '请输入剩余寿命' }]}
                >
                    <Input style={{ width: 100 }} type='number' onChange={this.setResidualLife} max={5}></Input>
                </Form.Item>
                <Form.Item>
                    {this.renderResidualLifeForm(this.state.residualLife)}
                    <Form.Item
                        label="折现率"
                        name="discountRate"
                        hidden={this.state.isQuantitativeValuation}
                        rules={[{ required: !this.state.isQuantitativeValuation, message: '请选择折现率' }]}
                    >
                        <Select style={{ width: 100 }}>
                            {discountRateArr.map((item, index) => {
                                return (
                                    <Option key={index}>{item}</Option>
                                )
                            })}
                        </Select>
                    </Form.Item>
                    <Button style={{ marginLeft: 508 }} type="primary" htmlType="submit">
                        计算
                        </Button>
                </Form.Item>
                {this.renderResult(this.state.isResult)}
            </Form>
        )
    }
}

const mapStateToProps = state => {
    let selectedKey = state.app.url
    return {
        selectedKey
    }
}

export default connect(mapStateToProps)(TechnicalValuationForm)